<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>部门管理</span>
      </div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="部门名称">
          <el-input v-model="searchForm.deptName" placeholder="部门名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="部门状态">
          <el-select v-model="searchForm.status" placeholder="请选择部门状态">
            <el-option label="正常" value="0"></el-option>
            <el-option label="停用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="doSearch">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="box-card">
      <el-button type="primary" style="margin-bottom: 10px" @click="handleAdd">添加部门</el-button>
      <el-table :data="deptList" 
        style="width: 100%" 
        border  
        row-key="deptId"
        default-expand-all>
        <el-table-column prop="deptName" label="部门名称"></el-table-column>
        <el-table-column prop="orderNum" label="排序"></el-table-column>
        <el-table-column prop="sex" label="状态">
          <template slot-scope="scope">
             {{scope.row.status==1?"停用":"正常"}}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">
              修改
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog :title="saveForm.deptId==null?'新增部门':'修改部门'" :visible.sync="dialogFormVisible">
        <el-form :model="saveForm" :rules="rules" ref="saveForm" :inline="true" label-width="90px">
          <el-form-item label="上级部门" prop="parentId" v-if="saveForm.parentId !== 0">
            <treeselect 
              v-model="saveForm.parentId" 
              :normalizer="normalizeOptions" 
              placeholder="请选择上级部门" 
              :options="deptOptions"
              style="width:480px">
            </treeselect>
          </el-form-item>
          <el-form-item label="部门名称" prop="deptName">
            <el-input v-model="saveForm.deptName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="显示排序" prop="orderNum">
            <el-input-number v-model="saveForm.orderNum" autocomplete="off" controls-position="right"></el-input-number>
          </el-form-item>
          <el-form-item label="负责人">
            <el-input v-model="saveForm.leader" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="saveForm.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="saveForm.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="部门状态">
            <el-radio-group v-model="saveForm.status">
              <el-radio label="0">正常</el-radio>
              <el-radio label="1">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="doSave">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: {
    Treeselect
  },
  data() {
    return {
      // 查询的数据模型
      searchForm: {
        //过滤条件
        deptName: null,
        status: null,
      },
      //表格显示的数据模型
      deptList:[],
      //下拉框显示的数据模型
      deptOptions:[],
      //控制对话框显示还是隐藏
      dialogFormVisible: false,
      //新增的数据模型
      saveForm: {
        deptId:null,
        parentId:null,
        deptName:"",
        orderNum:null,
        leader:"",
        phone:"",
        email:"",
        status:null
      },
      //验证数据模型
      rules: {
        parentId: [
          { required: true, message: "请选择上级部门", trigger: "blur" },
        ],
        deptName: [
          { required: true, message: "请输入部门名称！", trigger: "blur" },
        ],
        orderNum: [
          { required: true, message: "请输入排序号！", trigger: "blur" },
        ],
        email: [
          {type: 'email', message: '请输入正确的邮箱地址',trigger: 'blur'}
        ],
        phone: [
          {pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,message: '请输入正确的手机号码',trigger: 'blur'}
        ]
      },
    };
  },
  created() {
    //查询部门列表
    this.doSearch();
     //查询部门树
    this.doTreeSelect();
  },
  methods: {
    //查询部门列表
    doSearch() {
      this.$axios.get("/api/sys/dept/list", {params:this.searchForm})
        .then((res) => {
          if (res.data.code == 200) {
            this.deptList =  this.handleTree(res.data.data, 'deptId')
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //查询部门树
    doTreeSelect() {
      this.$axios.get("/api/sys/dept/treeselect")
        .then((res) => {
          if (res.data.code == 200) {
            this.deptOptions = res.data.data;
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //清空新增表单数据
    reset () {
      this.saveForm = {
        deptId:null,
        parentId:null,
        deptName:"",
        orderNum:null,
        leader:"",
        phone:"",
        email:"",
        status:null
      };
    },
    //添加
    handleAdd() {
      //1.清空模型数据
      this.reset();
      //2.显示对话框
      this.dialogFormVisible = true;
    },
    //保存
    doSave() {
      this.$refs["saveForm"].validate((vali) => {
        if (vali) {
          //发送ajax请求
          this.$axios.post("/api/sys/dept/save", this.saveForm)
            .then((res) => {
              if (res.data.code == 200) {
                //1.关闭对话框
                this.dialogFormVisible = false;
                //2.友好提示
                this.$message.success(res.data.msg);
                //3.刷新表格
                this.doSearch();
              } else {
                //1.友好提示
                this.$message.error(res.data.msg);
              }
            })
            .catch((err) => {
              console.log(err);
            });
        }
      });
    },
    //编辑
    handleEdit(index, row) {
      //1.清空模型数据
      this.reset();
      //2.查询要修改的数据
      this.$axios.get("/api/sys/dept/view/"+row.deptId).then((res)=>{
         if(res.data.code==200){  
            this.saveForm= res.data.data;    
         }else{
            this.$message.error(res.data.msg);
         }
      }).catch((err)=>{
        console.log(err);
      })
      //3.显示对话框
      this.dialogFormVisible=true;
    },
    //删除
    handleDelete(index, row) {
        this.$confirm('确定要删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //1.发送ajax请求
          this.$axios.delete("/api/sys/dept/del/"+row.deptId).then((res)=>{
              if (res.data.code == 200) {
                //1.友好提示
                this.$message.success(res.data.msg);
                //2.刷新表格
                this.doSearch();
              } else {
                //1.友好提示
                this.$message.error(res.data.msg);
              }
          }).catch((err)=>{
              console.log(err);
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 转换部门数据结构
    normalizeOptions(node) {
      if (node.children && !node.children.length) {
        // 去掉children=[]的children属性
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children,
      };
    },
  },
}
</script>

<style scoped>
</style>

